//
// Created by guanyicheng on 2022/5/17.
//

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */


#include "../../common.h"

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res{};
        if (root == nullptr) {
            return res;
        }
        stack<TreeNode *> st{};
        st.emplace(root);
        while (!st.empty()) {
            TreeNode *curr = st.top();
            st.pop();
            res.emplace_back(curr->val);
            if (curr->left != nullptr) {
                st.emplace(curr->left);
            }
            if (curr->right != nullptr) {
                st.emplace(curr->right);
            }
        }
        reverse(res.begin(), res.end());
        return res;
    }
};